package com.hxd.mall.utils.local

import android.annotation.SuppressLint
import android.content.Context
import android.content.SharedPreferences
import com.hxd.mall.constant.HxdGlobalInfo

/**
 * 缓存处理(不支持序列化，对象请存储为json串)
 */

object HxdSharePreferenceUtil {
    private var sharedPreferences: SharedPreferences? = null
    private var editor: SharedPreferences.Editor? = null
    private val map = mutableMapOf<String, Any>()

    fun saveStringData(key: String, value: String) {
        initSharePreference()
        map[key] = value
        editor!!.putString(key, value).commit()
    }

    fun saveIntData(key: String, value: Int) {
        initSharePreference()
        map[key] = value
        editor!!.putInt(key, value).commit()
    }

    fun saveBooleanData(key: String, value: Boolean) {
        initSharePreference()
        map[key] = value
        editor!!.putBoolean(key, value).commit()
    }

    fun getBooleanData(key: String): Boolean {
        return getBoolean(key, false)
    }

    fun getBooleanData(key: String, defaultValue: Boolean): Boolean {
        initSharePreference()

        return getBoolean(key, defaultValue)
    }

    fun getStringData(key: String): String {
        initSharePreference()

        return getString(key, "")
    }

    fun getStringData(key: String, defaultValue: String): String {
        initSharePreference()
        return getString(key, defaultValue)
    }

    fun getIntData(key: String): Int {
        initSharePreference()

        return getInt(key, 0)
    }

    fun getIntData(key: String, defaultValue: Int): Int {
        initSharePreference()

        return getInt(key, defaultValue)
    }

    fun deleteData(key: String) {
        initSharePreference()

        map.remove(key)
        editor!!.remove(key).commit()
    }

    fun clearAllData() {
        initSharePreference()

        map.clear()
        editor!!.clear().commit()
    }

    private fun getInt(key: String, defaultValue: Int): Int {
        return if (map.containsKey(key)) {
            map[key].toString().toInt()
        } else
            sharedPreferences!!.getInt(key, defaultValue)
    }

    private fun getString(key: String, defaultValue: String): String {
        return if (map.containsKey(key)) {
            map[key].toString()
        } else
            sharedPreferences!!.getString(key, defaultValue) ?: ""
    }

    private fun getBoolean(key: String, defaultValue: Boolean): Boolean {
        return if (map.containsKey(key)) {
            map[key].toString().toBoolean()
        } else
            sharedPreferences!!.getBoolean(key, defaultValue)
    }

    @SuppressLint("CommitPrefEdits")
    private fun initSharePreference() {
        if (sharedPreferences == null) {
            //val name = HxdGlobalInfo.instance.applicationContext.packageName
            sharedPreferences = HxdGlobalInfo.instance.applicationContext.getSharedPreferences("hxdmall", Context.MODE_PRIVATE)
        }
        if (editor == null) {
            editor = sharedPreferences!!.edit()
        }
    }

    /**
     * 用户信息不需要添加passportID，其他添加，区分用户切换账号缓存
     * 避免更换手机号导致读取不到缓存
     */
//    private fun getKey(key: String): String {
////        return when (key) {
////            HxdConstantsCache.Cache_Key_User_Info -> key
////            HxdConstantsCache.Cache_Key_Is_First_Open -> key + HxdGlobalInfo.versionCode
////            else -> key + HxdConstantsNet.userInfo?.passportID
////        }
//        return key
//    }
}


//class HxdPreferenceUtil<T>(val name: String, private val default: T) {
//    private val mSharedPreferences: SharedPreferences by lazy {
//        HxdGlobalInfo.instance.applicationContext.getSharedPreferences(
//                "hxd_cache",
//                Context.MODE_PRIVATE
//        )
//    }
//
//    @SuppressLint("CommitPrefEdits")
//    private fun putSharedPreferences(name: String, value: T) = with(mSharedPreferences.edit()) {
//        when (value) {
//            is Long -> putLong(name, value)
//            is String -> putString(name, value)
//            is Int -> putInt(name, value)
//            is Boolean -> putBoolean(name, value)
//            is Float -> putFloat(name, value)
//            else -> throw IllegalArgumentException("SharedPreferences can't be save this type")
//        }.apply()
//    }
//
//    @Suppress("UNCHECKED_CAST")
//    private fun getSharedPreferences(name: String, default: T): T = with(mSharedPreferences) {
//        val res: Any = when (default) {
//            is Long -> getLong(name, default)
//            is String -> getString(name, default)
//            is Int -> getInt(name, default)
//            is Boolean -> getBoolean(name, default)
//            is Float -> getFloat(name, default)
//            else -> throw IllegalArgumentException("SharedPreferences can't be get this type")
//        }
//
//        return res as T
//    }
//
//    /**
//     * 用户信息不需要添加passportID，其他添加，区分用户切换账号缓存
//     * 避免更换手机号导致读取不到缓存
//     */
//    operator fun getValue(thisRef: Any?, property: KProperty<*>): T {
//        return when (name) {
//            HxdConstantsCache.Cache_Key_User_Info -> getSharedPreferences(name, default)
//            HxdConstantsCache.Cache_Key_Is_First_Open -> getSharedPreferences(name + HxdGlobalInfo.versionCode, default)
//            else -> getSharedPreferences(name + HxdConstantsNet.userInfo?.passportID, default)
//        }
//    }
//
//    operator fun setValue(thisRef: Any?, property: KProperty<*>, value: T) {
//        when (name) {
//            HxdConstantsCache.Cache_Key_User_Info -> putSharedPreferences(name, value)
//            HxdConstantsCache.Cache_Key_Is_First_Open -> putSharedPreferences(name + HxdGlobalInfo.versionCode, value)
//            else -> putSharedPreferences(name + HxdConstantsNet.userInfo?.passportID, value)
//        }
//    }
//}